﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Bunian.Presenter;
using System.Collections.Generic;

namespace Bunian.Web.Common.Code
{
    public class PageBase : System.Web.UI.Page
    {
        private List<IDisposable> _pageLifeSpanningDisposables = new List<IDisposable>();

        protected T GetPageLifeSpanningObject<T>() where T : class, IDisposable, new()
        {
            T service = new T();

            _pageLifeSpanningDisposables.Add(service);

            return service;
        }

        protected override void OnUnload(EventArgs e)
        {
            base.OnUnload(e);

            foreach (IDisposable disposable in _pageLifeSpanningDisposables)
            {
                if (null != disposable)
                {
                    disposable.Dispose();
                }
            }
        }

        protected override void OnError(EventArgs e)
        {
            base.OnError(e);

            foreach (IDisposable disposable in _pageLifeSpanningDisposables)
            {
                if (null != disposable)
                {
                    disposable.Dispose();
                }
            }

            //TODO: Log Error
        }
    }
}
